package net.jueb.sniff4j.impl.http.filter.builder.request;

import java.util.HashMap;

import io.netty.channel.Channel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import net.jueb.sniff4j.extr.base.AbstractRuntime;
import net.jueb.sniff4j.extr.filter.builder.RBuilder;

public abstract class DefaultFullHttpRequestRBuilder extends RBuilder{

	public DefaultFullHttpRequestRBuilder(DefaultFullHttpRequest target,AbstractRuntime runtime, Channel fromChannel, Channel toChannel) {
		super(runtime, fromChannel, toChannel);
	}

	public  HashMap<String,String> uriToMap(String uri)
	{
		HashMap<String,String> map=new HashMap<String,String>();
		String s1=uri.trim();
		int a=uri.indexOf("?");
		if(a==(s1.length()-1))
		{//如果a是最后一位
			return map;
		}
		String s2=uri.substring(a+1);
		String[] s3=s2.split("&");
		if(s3==null || s3.length==0)
		{
			return map;
		}
		for(int i=0;i<s3.length;i++)
		{
			String[] s=s3[i].split("=");
			if(s!=null && s.length==1)
			{
				map.put(s[0],null);
			}else if(s!=null && s.length==2)
			{
				map.put(s[0], s[1]);
			}
		}
		return map;
	}
	
	public String mapToUri(HashMap<String,String> map)
	{
		StringBuffer sb=new StringBuffer();
		for(String k:map.keySet())
		{
			sb.append(k+"="+map.get(k)+"&");
		}
		return sb.toString();
	}
}
